﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus.Distributed;
using WMSSystem.DYB;
using WMSSystem.DYB.Dto.WarehouseManagement;
using WMSSystem.YSL.Interfance;

namespace WMSSystem.Handler.WarehouseManagementHandler
{
    public class CreateWarehouseHandler : IDistributedEventHandler<CreateWarehouseDto>, ITransientDependency
    {
        private readonly IRepository<WarehouseManagement, Guid> _warehouserepository;
        private readonly ICodeRuleAppService _codeRuleAppService;
        private readonly IMapper _mapper;
        public CreateWarehouseHandler(IRepository<WarehouseManagement, Guid> warehouserepository, ICodeRuleAppService codeRuleAppService, IMapper mapper)
        {
            _warehouserepository = warehouserepository;
            _codeRuleAppService = codeRuleAppService;
            _mapper = mapper;
        }
        public async Task HandleEventAsync(CreateWarehouseDto input)
        {
            var warehouse = _mapper.Map<WarehouseManagement>(input);
            warehouse.Code = await _codeRuleAppService.GenerateCodeAsync("warehouse");
            warehouse.CreatorId = new Guid("3a1b4d19-6f9c-7e5e-9f1f-ce1da55e4f1e");
            warehouse.Num = 0;
            warehouse.LastModificationTime = DateTime.Now;
            warehouse.LastModifierId = new Guid("3a1b4d19-6f9c-7e5e-9f1f-ce1da55e4f1e");
            await _warehouserepository.InsertAsync(warehouse);
        }
    }
}
